package com.zte.cl.service;

import org.apache.log4j.Logger;

import com.zte.cl.utils.ConstantUtil;
import com.zte.cl.utils.EHCacheUtil;
import com.zte.cl.utils.StringUtil;

public class CrawlerEHCacheUtil {
	public final static Logger log = Logger.getLogger(CrawlerEHCacheUtil.class);

	public static String cacheNewsKey(String channelId, String pageUrl, String title) {
		if (!StringUtil.isEmpty(pageUrl)) {
			return String.format("key_%s_%s", channelId, pageUrl);
		}
		if (!StringUtil.isEmpty(title)) {
			return String.format("key_%s_%s", channelId, title);
		}
		return null;
	}

	public static void putCached(String channelId, String pageUrl, String title) {
		EHCacheUtil.putObjectCached(ConstantUtil.CACHE_NAME, cacheNewsKey(channelId, pageUrl, title), channelId);
	}

	public static boolean ContainsKey(String channelId, String pageUrl, String title) {
		return EHCacheUtil.getObjectCached(ConstantUtil.CACHE_NAME, cacheNewsKey(channelId, pageUrl, title)) != null;
	}

	public static boolean isNewExistsByUrlOrTitle(String channelId, String pageUrl, String title) {
		return CrawlerEHCacheUtil.ContainsKey(channelId, pageUrl, title);
	}
	
	public static void removeCached(String channelId, String pageUrl, String title) {
		EHCacheUtil.remove(ConstantUtil.CACHE_NAME, cacheNewsKey(channelId, pageUrl, title));
	}
	
}
